package net.skyedu.utils;

public class Page{
	private int totalRows;	
	private int pageSize=15;	
	private int currentPage;	
	private int totalPages;		
	private int startRow;		
	
	public Page(int _totalRows){
		totalRows=_totalRows;		
		totalPages=totalRows/pageSize;
		int mod=totalRows%pageSize;
		if(mod>0){
			totalPages++;
		}
		currentPage = 1;
	    startRow = 0;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public int getStartRow() {
		return startRow;
	}
	
	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}
	
	public void first(){
		currentPage=1;
		startRow=0;
	}
	
	public void previous(){
		if(currentPage<=1){
			return ;
		}
		currentPage--;
		startRow=(currentPage-1)*pageSize;
	}
	
	public void next(){
		if(currentPage<totalPages){
			currentPage++;
		}
		startRow=(currentPage-1)*pageSize;
	}
	
	public void last(){		
		if(totalPages==0)
			return;
		currentPage=totalPages;
		startRow=(currentPage-1)*pageSize;		
	}
	
	public void refresh(int _currentPage){
		currentPage=_currentPage;
		if(totalPages!=0&&currentPage>totalPages){
			last();
		}else if(currentPage<=0){
			first();
		}else{
			startRow=(currentPage-1)*pageSize;
		}
	}

	@Override
	public String toString() {
		return "Page [totalRows=" + totalRows + ", pageSize=" + pageSize
				+ ", currentPage=" + currentPage + ", totalPages=" + totalPages
				+ ", startRow=" + startRow + "]";
	}
	
}
